package com.tvsinfo.jdk.demo.case1_grammar;

import com.tvsinfo.jdk.demo.case2_function_interface.dto.Car;

import java.util.Arrays;
import java.util.List;

/**
 * 行为参数化
 */
public class BehaviorParameterDemo {

    public static void main(String[] args) {
        Car a1 = new Car("奔驰A级", 45, "白色", 4700);
        Car a2 = new Car("奔驰C级", 25, "黑色", 4900);
        Car a3 = new Car("保时捷718", 60, "黄色", 4379);
        Car a4 = new Car("保时捷911", 400, "绿色", 4500);
        Car a5 = new Car("宝马 X1", 30, "蓝色", 4565);
        Car a6 = new Car("宝马 X3", 50, "白色", 4717);
        List<Car> cars = Arrays.asList(a1, a2, a3, a4, a5, a6);


        //第一种、方法
        //预算在40以下，蓝色的车
        Car temp=null;
        for (Car car : cars) {
            if (car.getColor().equals("白色") && car.getPrice() >= 50 && car.getPrice() <= 60) {
                System.out.println(car.getBrand());
                temp=car;
                break;
            }
        }
        System.out.println(temp);


        //品牌宝马，长度在4700以上
        for (Car car : cars) {
            if (car.getBrand().contains("宝马") && car.getPrice() > 4700) {
                System.out.println(car.getBrand());
                temp=car;
                break;
            }
        }

        System.out.println(temp);



        //第二种 通过内部类判断
        CarPredicate carPredicate = new CarPredicate() {
            @Override
            public boolean test(Car car) {
                return car.getColor().equals("白色") && car.getPrice() >= 50 && car.getPrice() <= 60;
            }
        };
        Car car = getMyCar(cars, carPredicate);

        //
        CarPredicate carPredicate2 = new CarPredicate() {
            @Override
            public boolean test(Car car) {
                return car.getBrand().contains("宝马") && car.getPrice() > 4700;
            }
        };
        Car car2 = getMyCar(cars, carPredicate2);



        //第三种,行为通过参数传递
        Car car3 = getMyCar(cars, car1 -> car.getColor().equals("白色") && car.getPrice() >= 50 && car.getPrice() <= 60);
        Car car4 = getMyCar(cars, car1 -> car.getBrand().contains("宝马") && car.getPrice() > 4700);

    }


    private static Car getMyCar(List<Car> cars, CarPredicate carPredicate) {
        for (Car car : cars) {
            if (carPredicate.test(car)) {
                return car;
            }
        }
        return null;
    }

    interface CarPredicate {
        boolean test(Car car);
    }

}
